//
//  YJFindThePassWordViewController.m
//  Sports-Go
//
//  Created by rimi on 16/9/23.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "YJFindThePassWordViewController.h"

@interface YJFindThePassWordViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UIView *xian;
@property (nonatomic,strong) UITextField *mailBox;
@end

@implementation YJFindThePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:RzCGRectMake(0, 0, 375, 667)];
    
    backImage.image = [UIImage imageNamed:@"loginback"];
    [self.view addSubview:backImage];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:RzCGRectMake(30, 50, 50, 30)];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backtheLastPage) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:backButton];
    
    _mailBox = [[UITextField alloc] initWithFrame:RzCGRectMake(50, 250, 275, 50)];
    _mailBox.clearButtonMode = UITextFieldViewModeWhileEditing;
    _mailBox.placeholder = @"请输入您的邮箱";
    _mailBox.delegate = self;
    _xian = [[UIView alloc] initWithFrame:RzCGRectMake(50, 300, 275, 1)];
    _xian.backgroundColor = [UIColor whiteColor];
    
    UIButton *sureButton = [[UIButton alloc] initWithFrame:RzCGRectMake(137, 370, 100, 30)];
    sureButton.backgroundColor = [UIColor lightGrayColor];
    sureButton.tag = 444;
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(findThePassWord) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_mailBox];
    [self.view addSubview:_xian];
    [self.view addSubview:sureButton];

}

#pragma mark -- uitextfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    _xian.backgroundColor = [UIColor blueColor];
    UIButton *sureButt = [self.view viewWithTag:444];
    sureButt.backgroundColor = getColor(58, 206, 248, 1);
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    
    return YES;
}
#pragma mark -- button click
- (void)backtheLastPage{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)findThePassWord{
   

    if (_mailBox.text.length > 0) {
        
      BOOL ifMail =  [self validateEmail:_mailBox.text];
        if (ifMail) {
            
            [AVUser requestPasswordResetForEmailInBackground:_mailBox.text block:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                //    NSLog(@"%d",succeeded);
                    [self warnWitherrorInfo:@"发送成功,请前去邮箱修改密码"];
                    [self performSelector:@selector(backtheLastPage) withObject:self afterDelay:2];
                    
                } else {
                    [self warnWitherrorInfo:@"找回密码失败"];
                }
            }];

        }else{
            _mailBox.text = @"";
            [self warnWitherrorInfo:@"请输入正确的邮箱"];
    
        }
        
    }else{
        
        [_mailBox becomeFirstResponder];
        [self warnWitherrorInfo:@"输入不可为空"];
    }
}
//邮箱
- (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

#pragma mark -- alter
- (void)warnWitherrorInfo:(NSString *)error{
    //警示框
    UILabel *label = [[UILabel alloc]initWithFrame:RzCGRectMake(100, 270, 185, 50)];
    label.backgroundColor = [UIColor lightGrayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.text = error;
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [UIView animateWithDuration:2.0 animations:^{
        label.alpha = 0.0;
    } completion:^(BOOL finished) {
        
        [label removeFromSuperview];
    }];
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
